package uk.ac.cam.bt288.DesktopApp;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;

public class MainInterface extends JFrame {

	private static final long serialVersionUID = 1L;
	private static JLabel status = null;

	MainInterface() {
		super("Pico Application");               //create window & set title text
		setDefaultCloseOperation(EXIT_ON_CLOSE); //close button on window quits app.
		setSize(640,480);

		JComponent qrCode = qrPanel();
		add(qrCode, BorderLayout.WEST);


		JComponent pwBox = pwPanel();
		add(pwBox, BorderLayout.EAST);

		status = statusPanel();
		add(status, BorderLayout.SOUTH);

	}

	private JComponent pwPanel () {
		Box vb = Box.createVerticalBox();
		JLabel app = new JLabel("Pico Demo Application");
		vb.add(app);
		vb.add(Box.createVerticalStrut(100));
		JPanel result1 = new JPanel();
		JPanel result2 = new JPanel();
		JTextField username = new JTextField();
		JPasswordField password = new JPasswordField();
		JLabel    usernamelabel= new JLabel("Username");
		JLabel    passwordlabel= new JLabel("Password");
		username.setBounds(5, 5, 100, 100);
		password.setBounds(105, 105, 100, 100);
		username.setPreferredSize(new Dimension(80,30));
		password.setPreferredSize(new Dimension(80,30));
		result1.add(usernamelabel);
		result1.add(username);
		vb.add(result1);
		result2.add(passwordlabel);
		result2.add(password);
		vb.add(result2);
		
		JButton login = new JButton("Log In");
		vb.add(Box.createVerticalStrut(100));
		JButton newAcc = new JButton("Create new account");
		vb.add(login);
		vb.add(newAcc);

		return vb;
	}

	private JLabel statusPanel () {
		JLabel currStatus = new JLabel("Ready!");
		currStatus.setBackground(Color.GREEN);
		addBorder(currStatus, "Status");
		return currStatus;
	}

	private JComponent qrPanel () {
		BufferedImage myPicture = null;
		try {
			myPicture = ImageIO.read(new File("qr_png.png"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		JLabel picLabel = new JLabel(new ImageIcon( myPicture ));
		addBorder(picLabel, "QR Code");

		return picLabel;
	}

	private void addBorder(JComponent component, String title) {
		Border etch = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
		Border tb = BorderFactory.createTitledBorder(etch,title);
		component.setBorder(tb);
	}


	public static void main(String[] args) {
		MainInterface hello = new MainInterface(); //create instance 
		hello.setVisible(true);                        //display window to user
		Thread bt = new Thread(new BluetoothThread(status)); //start Bluetooth socket
		bt.start();
	}


}
